﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ViewModel;
using DataLayer;

namespace Service
{
    public class Sticky
    {
        public static int GetCount(Status st, bool countAll = false)
        {
            int count = 0;
            try
            {
                using (iTrackerEntities DB = new iTrackerEntities())
                {
                    count = (from i in DB.issues
                             join a in DB.assignments on i.id equals a.issue_id
                             where a.is_active && !a.is_deleted && i.status_id == (int)st
                             && a.is_active && a.shop_id == (((int)ActiveSession.User.Role == 1 || countAll) ? a.shop_id.Value : ActiveSession.User.Shop.ID)
                             select i).Count();
                }
            }
            catch (Exception ex)
            {
                Helper.ErrorLogger.WriteLog("GetCount", ex);
                throw;
            }
            return count;
        }

        public static List<Issue> GetNewlyAssignedIssues(int count = 1)
        {
            List<Issue> items = new List<Issue>();
            try
            {
                using (iTrackerEntities DB = new iTrackerEntities())
                {
                    items = (from i in DB.issues
                             join a in DB.assignments on i.id equals a.issue_id
                             join s in DB.shops on a.shop_id equals s.id
                             where (i.status_id == 1 || i.status_id == 2)
                             && a.is_active && a.shop_id ==  ActiveSession.User.ShopID
                             select new Issue()
                             {
                                 ID = i.id,
                                 Name = i.name,
                                 Description = i.description,
                                 Priority = (Priority)i.priority_id,
                                 Status = (Status)i.status_id,
                                 DateCreated = i.date_created,
                                 DateStart = i.date_start,
                                 DateEnd = i.date_end,
                                 Assignee = new List<Shop>() { new Shop() { ID = s.id, Name = s.name, UDF1 = s.udf1 } },
                                 DateCompleted = a.date_updated
                             })
                             .OrderByDescending(x => x.ID)
                            .Take(count)
                         .ToList();
                }
            }
            catch (Exception ex)
            {
                Helper.ErrorLogger.WriteLog("GetCount", ex);
                throw;
            }
            return items;
        }

        public static List<Issue> GetNewlyAssignedIssues(List<int> issueIds)
        {
            List<Issue> items = new List<Issue>();
            try
            {
                using (iTrackerEntities DB = new iTrackerEntities())
                {
                    items = (from i in DB.issues.Where(x=> issueIds.Contains(x.id)).ToList()
                             join a in DB.assignments on i.id equals a.issue_id
                             join s in DB.shops on a.shop_id equals s.id
                             where a.is_active
                             select new Issue()
                             {
                                 ID = i.id,
                                 Name = i.name,
                                 Description = i.description,
                                 Priority = (Priority)i.priority_id,
                                 Status = (Status)i.status_id,
                                 DateCreated = i.date_created,
                                 DateStart = i.date_start,
                                 DateEnd = i.date_end,
                                 Assignee = new List<Shop>() { new Shop() { ID = s.id, Name = s.name, UDF1 = s.udf1 } },
                                 DateCompleted = a.date_updated
                             })
                             .OrderByDescending(x => x.ID)
                         .ToList();
                }
            }
            catch (Exception ex)
            {
                Helper.ErrorLogger.WriteLog("GetNewlyAssignedIssues", ex);
                throw;
            }
            return items;
        }
    }
}
